home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CDTV / cdtvtools-11 / system / bookmark.c next >
Encoding:
C/C++ Source or Header  |  1991-06-24  |  6.5 KB  |  244 lines

  1. /***********************************************************************
  2. ***
  3. *** CDTV - Write a Bookmark
  4. ***
  5. ***     Copyright (C) 1990 Commodore-Amiga, Inc.
  6. ***     Permission granted for use in CDTV applications.
  7. ***     Author: Carl Sassenrath, Ukiah, CA  (30-DEC-90)
  8. ***
  9. ************************************************************************
  10. ***
  11. ***     What:
  12. ***       This example will create a new bookmark (if one with
  13. ***       the same ID does not exist) and write sample data
  14. ***       into the bookmark.  It will overwrite an existing
  15. ***       bookmark if one is present.
  16. ***
  17. ***     Notes:
  18. ***       1. You will need to obtain a Bookmark Manufacturer
  19. ***          identification number from Commodore before creating
  20. ***          bookmarks of your own.  This is very easy to do,
  21. ***          just call Commodore (CATS).
  22. ***       2. Currently tested with Manx C compiler only.
  23. ***
  24. ***     Please rush bugs, comments, or suggestions to:
  25. ***       Carl Sassenrath, P.O. Box 1510, Ukiah, CA 95482
  26. ***
  27. ***
  28. ***********************************************************************/
  29.  
  30. #include <exec/types.h>
  31. #include <exec/io.h>
  32. #include <devices/bookmark.h>
  33.  
  34. extern  struct  IOStdReq *CreateStdIO();
  35. extern  struct  MsgPort  *CreatePort();
  36. struct  IOStdReq *IOReq1 = NULL;
  37. struct  MsgPort  *IOPort = NULL;
  38.  
  39. char    AssertFail[] = "Assertion failed (MUST)";
  40. #define MUST(expr)  if (!(expr)) Quit(AssertFail);
  41.  
  42. /*
  43. ** Bookmark ID
  44. **
  45. **      This is an example ID.  The upper word is the manufacturer id,
  46. **      the lower is the product code.  You will need to use your own
  47. **      manufacturer ID.
  48. */
  49. #define APP_BOOKMARK    MAKEBID(5,1)
  50.  
  51. /*
  52. ** Bookmark Data
  53. **
  54. **      This is the data structure you wish to use for your bookmark.
  55. **      It can be of any form you desire.
  56. */
  57. struct  AppStruct
  58. {
  59.         ULONG   AField;
  60.         WORD    BField;
  61.         char    Name[32];
  62. } AppBookmark;
  63.  
  64. /***********************************************************************
  65. ***
  66. ***  Main
  67. ***
  68. ***********************************************************************/
  69. main(argc,argv)
  70.         int argc;
  71.         char *argv[];
  72. {
  73.         printf("CDTV Bookmark Example (30-Dec-90)\n");
  74.  
  75.         if (argc < 2)
  76.         {
  77.                 puts("Usage: bookmark <action>");
  78.                 puts("Where <action> is:");
  79.                 puts("   W - to write/create a bookmark");
  80.                 puts("   R - to read a bookmark");
  81.                 puts("   D - to delete a bookmark");
  82.                 puts("   M - bookmark memory available");
  83.                 puts("   S - maximum bookmark size");
  84.                 Quit("missing action");
  85.         }
  86.  
  87.         Init();
  88.  
  89.         switch (*argv[1])
  90.         {
  91.         case 'W':
  92.         case 'w':  WriteBookmark(); break;
  93.  
  94.         case 'R':
  95.         case 'r':  ReadBookmark(); break;
  96.  
  97.         case 'D':
  98.         case 'd':  DeleteBookmark(); break;
  99.  
  100.         case 'M':
  101.         case 'm':  AvailBookmark(); break;
  102.  
  103.         case 'S':
  104.         case 's':  MaxSizeBookmark(); break;
  105.  
  106.         default:   Quit("invalid action specified");
  107.         }
  108.  
  109.         Quit(0);
  110. }
  111.  
  112. /***********************************************************************
  113. ***
  114. ***  Bookmark Operations
  115. ***
  116. ***********************************************************************/
  117. WriteBookmark()
  118. {
  119.         int err;
  120.  
  121.         /* Create the bookmark.... */
  122.         err = DoIOR(IOReq1, BD_CREATE, APP_BOOKMARK, sizeof(AppBookmark), 0);
  123.         if (err) switch (err)
  124.         {
  125.         case BDERR_EXISTS:  puts("Bookmark exists"); break;
  126.         case BDERR_TOOBIG:  Quit("Boomark is too large");
  127.         case BDERR_NOSPACE: Quit("Not enough memory for bookmark");
  128.         default:
  129.                 printf("DoIO error: %d\n",err);
  130.                 Quit("BD_CREATE failed");
  131.         }
  132.         else puts("Bookmark created");
  133.  
  134.         /* Write example data to bookmark... */
  135.         AppBookmark.AField = 12345;
  136.         AppBookmark.BField = 1<<10;
  137.         strcpy(AppBookmark.Name,"important string");
  138.         if (DoIOR(IOReq1, CMD_WRITE, 0, sizeof(AppBookmark), &AppBookmark))
  139.                 Quit("CMD_WRITE failed");
  140.  
  141.         puts("Bookmark written");
  142. }
  143.  
  144. ReadBookmark()
  145. {
  146.         int err;
  147.  
  148.         err = DoIOR(IOReq1, CMD_READ, 0, sizeof(AppBookmark), &AppBookmark);
  149.         if (err)
  150.         {
  151.                 if (err == BDERR_NOMARK) Quit("Bookmark does not exist");
  152.                 printf("DoIO error: %d\n",err);
  153.                 Quit("CMD_READ failed");
  154.         }
  155.  
  156.         printf("Bookmark contents: %d 0x%x \"%s\"\n",
  157.                 AppBookmark.AField,
  158.                 AppBookmark.BField,
  159.                 AppBookmark.Name);
  160. }
  161.  
  162. DeleteBookmark()
  163. {
  164.         int err;
  165.  
  166.         err = DoIOR(IOReq1, BD_DELETE, 0, 0, 0);
  167.         if (err)
  168.         {
  169.                 if (err == BDERR_NOMARK) Quit("Bookmark does not exist");
  170.                 printf("DoIO error: %d\n",err);
  171.                 Quit("BD_DELETE failed");
  172.         }
  173.  
  174.         puts("Bookmark deleted");
  175. }
  176.  
  177. AvailBookmark()
  178. {
  179.         if (DoIOR(IOReq1, BD_AVAIL, 0, 0, 0))
  180.                 Quit("BD_AVAIL failed");
  181.  
  182.         printf("%d bytes available\n",IOReq1->io_Actual);
  183. }
  184.  
  185. MaxSizeBookmark()
  186. {
  187.         if (DoIOR(IOReq1, BD_MAXSIZE, 0, 0, 0))
  188.                 Quit("BD_MAXSIZE failed");
  189.  
  190.         printf("Maximum bookmark size is %d bytes\n",IOReq1->io_Actual);
  191. }
  192.  
  193. /***********************************************************************
  194. ***
  195. ***  Init -- initialize program and structures
  196. ***
  197. ***********************************************************************/
  198. Init()
  199. {
  200.         MUST(IOPort = CreatePort(0,0));
  201.         MUST(IOReq1 = CreateStdIO(IOPort));
  202.  
  203.         if (OpenDevice("bookmark.device",APP_BOOKMARK,IOReq1,0))
  204.                 Quit("Bookmark device will not open");
  205. }
  206.  
  207. /***********************************************************************
  208. ***
  209. ***  Quit -- exit program and clean-up.  Return an error if needed.
  210. ***
  211. ***********************************************************************/
  212. Quit(s)
  213.         char *s;        /* error message */
  214. {
  215.         if (IOReq1)
  216.         {
  217.                 if (IOReq1->io_Device) CloseDevice(IOReq1);
  218.                 DeleteStdIO(IOReq1);
  219.         }
  220.         if (IOPort)     DeletePort(IOPort);
  221.         if (s) {printf("\nERROR: %s\n",s); exit(40);}
  222.         else exit(0);
  223. }
  224.  
  225. /***********************************************************************
  226. ***
  227. ***  DoIOR -- execute a device command
  228. ***
  229. ***********************************************************************/
  230. DoIOR(req,cmd,off,len,data)
  231.         struct IOStdReq *req;
  232.         int cmd;
  233.         long off;
  234.         long len;
  235.         APTR data;
  236. {
  237.         req->io_Command = cmd;
  238.         req->io_Offset = off;
  239.         req->io_Length = len;
  240.         req->io_Data   = data;
  241.         return DoIO(req);
  242. }
  243.  
  244.